package com.xj0927.springcloud.controller;

import com.xj0927.springcloud.entities.CommonResult;
import com.xj0927.springcloud.entities.Payment;
import com.xj0927.springcloud.lb.LoadBalancer;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import java.net.URI;
import java.util.List;

/**
 * @Author: xj0927
 * @Description:
 * @Date Created in 2020-10-22 20:14
 * @Modified By:
 */
@RestController
@Slf4j
public class OrderController {

    //单机版：写死了访问的具体服务地址【public static final String PAYMENT_URL = "http://127.0.0.1:8001";】
    //集群版：访问服务名称即可
    public static final String PAYMENT_URL = "http://CLOUD-PAYMENT-SERVICE";

    @Autowired
    private RestTemplate restTemplate;

    //自定义负载均衡算法
    @Resource
    private LoadBalancer loadBalancer;
    @Resource
    private DiscoveryClient discoveryClient;

    @GetMapping("/consumer/payment/create") //浏览器只能发送get请求：但实质服务器端还是发送的post请求
    public CommonResult<Payment> create(Payment payment) {
        return restTemplate.postForObject(PAYMENT_URL + "/payment/create", payment, CommonResult.class);
    }

    @GetMapping("/consumer/payment/get/{id}")
    public CommonResult<Payment> get(@PathVariable Long id) {
        //getForObject 只是返回json串
        return restTemplate.getForObject(PAYMENT_URL + "/payment/get/" + id, CommonResult.class);
    }

    @GetMapping("/consumer/payment/getEntity/{id}")
    public CommonResult<Payment> get2(@PathVariable Long id) {
        //getForEntity 返回对象包括头信息、状态码、实体内容等等
        ResponseEntity<CommonResult> entity = restTemplate.getForEntity(PAYMENT_URL + "/payment/get/" + id, CommonResult.class);
        if (entity.getStatusCode().is2xxSuccessful()) {//判断状态码
            return entity.getBody();
        } else {
            return new CommonResult(444, "查询失败");
        }
    }

    @GetMapping("/consumer/lb")
    public String getMyLb() {
        List<ServiceInstance> instances = discoveryClient.getInstances("CLOUD-PAYMENT-SERVICE");
        if (instances == null || instances.size() <= 0) {
            return null;
        } else {
            ServiceInstance instance = loadBalancer.instance(instances);
            URI uri = instance.getUri();
            return restTemplate.getForObject(uri + "/payment/lb", String.class);
        }
    }

    // ====================> zipkin+sleuth
    @GetMapping("/consumer/payment/zipkin")
    public String paymentZipkin(){
        String result = restTemplate.getForObject("http://localhost:8001"+"/payment/zipkin/", String.class);
        return result;
    }
}
